<%@ Page language="vb" CodeFile="searchVehicle.aspx.vb" Inherits="searchVehicle_aspx_vb" %>

<%
    rowNumber = 0
    If (Request.QueryString("do")) = Nothing Then
        subDo = Request.QueryString("subDo")
        strTop50 = Request.QueryString("strTop50")
        If Len(strTop50) = 0 Then
            strTop50 = 1
        End If
        If CInt(strTop50) = 1 Then
            sqlTop50 = "Top 50"
        Else
            sqlTop50 = ""
        End If
        Over()
        dbOpen()
        RecSet = Conn.Execute("Select partnerID, partnerName, countryID From tblPartner Order by partnerName")
        If Not RecSet.EOF Then
            Do Until RecSet.EOF
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryID = RecSet.Fields("countryID").Value
                strSelectSubJS = strSelectSubJS + "new Array(" & partnerID & "," & countryID & ",""" & partnerName & """),"
                RecSet.MoveNext()
            Loop
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select countryID From tblCountry Order by countryID")
        Do Until RecSet.EOF
            countryID = RecSet.Fields("countryID").Value
            strSelectSubJS = strSelectSubJS + "new Array(0," & countryID & ","" ------------------------------------  ""),"
            RecSet.MoveNext()
        Loop
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        strSelectSubJS = Left(strSelectSubJS, Len(strSelectSubJS) - 1)
        strSelectSubHeadline = "Select partner"
        selectSub()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>SEARCH LEASE CONTRACT<hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='searchVehicle.aspx?subDo=search' method='post' name='store_productsSearch'>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Country:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='countryID' id='countryID' onchange=""set_child_listbox(this, document.store_productsSearch.partnerID,Product,'Products');"">")
        Response.Write("<option value=''> Select country  ")
        dbOpen()
        RecSet = Conn.Execute("Select countryID, country From tblCountry")
        If Not RecSet.EOF Then
            Do Until RecSet.EOF
                countryID = RecSet.Fields("countryID").Value
                country = RecSet.Fields("country").Value
                Response.Write("<option value='" & countryID & "'> " & country & " ")
                RecSet.MoveNext()
            Loop
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Partner:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='partnerID' id='partnerID' disabled>")
        Response.Write("<option value=''> - Select partner - ")
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Car make:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='brandID' id='brandID'>")
        Response.Write("<option value=''> Select car make  ")
        dbOpen()
        RecSet = Conn.Execute("Select brandID, brand From tblBrand")
        If Not RecSet.EOF Then
            Do Until RecSet.EOF
                brandID = RecSet.Fields("brandID").Value
                brand = RecSet.Fields("brand").Value
                Response.Write("<option value='" & brandID & "'> " & brand & " ")
                RecSet.MoveNext()
            Loop
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_brandID' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr><td class='formBold'>Licence number:</td><td align='right'><input type='text' name='regnr' id='regnr' size='40' maxlength='50'></td><td><div id='error_regnr' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>VIN-number:</td><td align='right'><input type='text' name='vinNumber' id='vinNumber' size='40' maxlength='50'></td><td><div id='error_vinNumber' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Contract number:</td><td align='right'><input type='text' name='contractNumber' id='contractNumber' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Search'></td></tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td><br></td></tr>")
        countryID = Request.Form("countryID")
        partnerID = Request.Form("partnerID")
        brandID = Request.Form("brandID")
        regnr = Request.Form("regnr")
        vinNumber = Request.Form("vinNumber")
        contractNumber = Request.Form("contractNumber")
        If Len(countryID) = 0 And Len(partnerID) = 0 And Len(brandID) = 0 And Len(regnr) = 0 And Len(vinNumber) = 0 And Len(contractNumber) = 0 Then
            countryID = Request.QueryString("countryID")
            partnerID = Request.QueryString("partnerID")
            brandID = Request.QueryString("brandID")
            regnr = Request.QueryString("regnr")
            vinNumber = Request.QueryString("vinNumber")
            contractNumber = Request.QueryString("contractNumber")
        End If
        If Len(countryID) > 0 Then
            countryID = CLng(countryID)
            sqlCountryID = " AND v.countryID = " & countryID & ""
        End If
        If Len(partnerID) > 0 Then
            partnerID = CLng(partnerID)
            sqlPartnerID = " AND v.partnerID = " & partnerID & ""
        End If
        If Len(brandID) > 0 Then
            brandID = CLng(brandID)
            sqlBrandID = " AND v.brandID = " & brandID & ""
        End If
        regnr = CleanString(regnr)
        sqlRegnr = " AND v.regnr Like '%" & regnr & "%'"
        vinNumber = CleanString(vinNumber)
        sqlVinNumber = " AND v.vinNumber Like '%" & vinNumber & "%'"
        contractNumber = CleanString(contractNumber)
        sqlContractNumber = " AND v.contractNumber Like '%" & contractNumber & "%'"
        If Len(subDo) = 0 Then
            sqlCountryID = " And v.countryID = 1000"
        End If
        dbOpen()
        RecSet = Conn.Execute("Select Count(v.vehicleID) As vehicleCount From tblVehicle v Where v.vehicleID > 0" & sqlCountryID & sqlPartnerID & sqlBrandID & sqlRegnr & sqlVinNumber & sqlContractNumber & "")
        vehicleCount = RecSet.Fields("vehicleCount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr>")
        Response.Write("<td valign='top' colspan='3'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='100%'>")
        Response.Write("<tr>")
        Response.Write("<td class='headline' colspan='10'>")
        Response.Write("SEARCH RESULT")
        If CInt(strTop50) = 1 And subDo = "search" And CDbl(vehicleCount) > 50 Then
            Response.Write("- LATEST 50")
            Response.Write(" <a href='searchVehicle.aspx?strTop50=0&subDo=search&countryID=" & countryID & "&partnerID=" & partnerID & "&brandID=" & brandID & "&regnr=" & regnr & "&vinNumber=" & vinNumber & "&contractNumber=" & contractNumber & "'>[SHOW ALL " & vehicleCount & " CONTRACTS]</a>")
        End If
        Response.Write("<hr class='hrHeadline'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        dbOpen()
        Dim Marke
        Dim Modell
        'sql = "Select " & sqlTop50 & " v.vehicleID, v.regnr, v.vinNumber, v.contractNumber, v.date AS tblVehicleDate, v.completed, v.active, p.product, b.brand, m.model, d.dealer, c.country From (((((tblVehicle v INNER JOIN tblProduct p ON p.productID = v.productID) INNER JOIN tblBrand b ON b.brandID = v.brandID) INNER JOIN tblModel m ON m.modelID = v.modelID) INNER JOIN tblDealer d ON d.dealerID = v.dealerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where v.vehicleID > 0" & sqlCountryID & sqlPartnerID & sqlBrandID & sqlRegnr & sqlVinNumber & sqlContractNumber & " ORDER BY v.vehicleID Desc"
        sql = "Select " & sqlTop50 & " v.vehicleID, v.regnr, v.vinNumber, v.contractNumber, v.date AS tblVehicleDate, v.completed, v.active, b.brandID, m.modelID,p.product, VehicleInfo.Marke,VehicleInfo.Modell, b.brand, m.model,pl.priceListID, d.dealer, c.country  From (((((tblVehicle v Full JOIN tblProduct p ON p.productID = v.productID) full join tblPriceList as pl on pl.priceListID = v.priceListID full Join VehicleInfo on vehicleInfo.Typkod = pl.Typkod Full JOIN tblBrand b ON b.brandID = v.brandID) Full JOIN tblModel m ON m.modelID = v.modelID) Full JOIN tblDealer d ON d.dealerID = v.dealerID) Full JOIN tblCountry c ON c.countryID = v.countryID) Where v.vehicleID > 0" & sqlCountryID & sqlPartnerID & sqlBrandID & sqlRegnr & sqlVinNumber & sqlContractNumber & " ORDER BY v.vehicleID Desc"
        RecSet = Conn.Execute(sql)
       
        If RecSet.EOF Then
            Response.Write("<tr><td><font color='red'>No lease contracts found.</font></td></tr>")
        Else
            Response.Write("<tr bgcolor='#eeeeee'>")
            Response.Write("<td class='formBold'>Car dealer</td>")
            Response.Write("<td class='formBold'>Contract number</td>")
            Response.Write("<td class='formBold'>Licence number</td>")
            Response.Write("<td class='formBold'>VIN-number</td>")
            Response.Write("<td class='formBold'>Car make</td>")
            Response.Write("<td class='formBold'>Product</td>")
            Response.Write("<td class='formBold'>Country</td>")
            Response.Write("<td class='formBold'>Status</td>")
            Response.Write("<td class='formBold'>First created</td>")
            Response.Write("<td class='formBold'></td>")
            Response.Write("</tr>")
            rowNumber = 1
            Do Until RecSet.EOF
                vehicleID = RecSet.Fields("vehicleID").Value
                regnr = RecSet.Fields("regnr").Value
                vinNumber = RecSet.Fields("vinNumber").Value
                contractNumber = RecSet.Fields("contractNumber").Value
                Marke = RecSet.Fields("Marke").Value
                Modell = RecSet.Fields("Modell").Value
                'brandID = RecSet.Fields("brandID").Value
                'modelID = RecSet.Fields("modelID").Value
                tblVehicleDate = RecSet.Fields("tblVehicleDate").Value
                completed = RecSet.Fields("completed").Value
                active = RecSet.Fields("active").Value
                product = RecSet.Fields("product").Value
                Dim brandi
                Dim modeli
                If vehicleID = 5827 Then
                    brandi = "Honda"
                    modeli = "Civic"
                    brand = brandi
                    model = modeli
                Else
                    brand = RecSet.Fields("brand").Value
                    model = RecSet.Fields("model").Value
                End If
                If brand = "-1" And model = "-1" Then
                    brand = RecSet.Fields("Marke").Value
                    model = RecSet.Fields("Modell").Value
                Else
                    brand = RecSet.Fields("brand").Value
                    model = RecSet.Fields("model").Value
                End If
                dealer = RecSet.Fields("dealer").Value
                country = RecSet.Fields("country").Value
                If rowNumber = 0 Then
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End If
                If CDbl(completed) = 1 Then
                    status = "<font color='orange'>Completed</font>"
                ElseIf CDbl(active) = 0 Then
                    status = "<font color='red'>Inactive</font>"
                Else
                    status = "<font color='green'>Active</font>"
                End If
                
                
                '
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & dealer & "</td>")
                Response.Write("<td>" & contractNumber & "</td>")
                Response.Write("<td>" & regnr & "</td>")
                Response.Write("<td>" & vinNumber & "</td>")
                Response.Write("<td>" & brand & " " & model & "</td>")
                Response.Write("<td>" & product & "</td>")
                Response.Write("<td>" & country & "</td>")
                Response.Write("<td>" & status & "</td>")
                Response.Write("<td>" & Left(tblVehicleDate, 10) & "</td>")
                Response.Write("<td><a href='editVehicle.aspx?vehicleID=" & vehicleID & "'>view details</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='10'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    End If
%>



